package org.example.helloworld.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.helloworld.entity.ElcPri;
import org.example.helloworld.entity.Result;
import org.example.helloworld.service.IElcPriService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 电费价格 前端控制器
 * </p>
 *
 * @author cwf
 * @since 2024-07-10
 */
@RestController
@RequestMapping("/elcPri")
public class ElcPriController {

    @Autowired
    private IElcPriService IElcPriService;

//    @GetMapping
//    public List<ElcPri> getAllRates() {
//        return IElcPriService.list();
//    }

    @GetMapping("/{id}")
    public ElcPri getRateById(@PathVariable String id) {
        return IElcPriService.getById(id);
    }

    @PostMapping("/add")
    public boolean addRate(@RequestBody ElcPri rate) {
        return IElcPriService.save(rate);
    }

    @PutMapping("/update")
    public boolean updateRate(@RequestBody ElcPri rate) {
        return IElcPriService.updateById(rate);
    }

    @DeleteMapping("/{id}")
    public boolean deleteRate(@PathVariable String id) {
        return IElcPriService.removeById(id);
    }

    @GetMapping("/search")
    public List<ElcPri> searchRates(@RequestParam String step) {
        QueryWrapper<ElcPri> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("step", step);
        return IElcPriService.list(queryWrapper);
    }

    @GetMapping
    public IPage<ElcPri> getAllRates(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "5") int size,
            @RequestParam(value = "search", required = false) String search) {
        Page<ElcPri> elcPriPage = new Page<>(page, size);
        IPage<ElcPri> resultPage = IElcPriService.getElcPriList(elcPriPage, search);
        return resultPage;
    }


}
